function output=corchanneltest3(needleng,temp_noise1,temp_noise2,dot,p,ratio,number,Tr,Rr,tx,rx,path_num,path_th,K)
         
persistent leftnum;
persistent temp1;
persistent temp2;
%persistent loc;
%persistent beginloc;
persistent count;

% if ((K==1)&((tx-1)*Rr+rx==1)&(path_th==1))==1 Midified on May 7 2008
if ((K==0)&((tx-1)*Rr+rx==1)&(path_th==1))==1
    count=ones(Tr*Rr,path_num);
    leftnum=zeros(Tr*Rr,path_num);
    temp1=[];
    temp2=[];
end

if(count((tx-1)*Rr+rx,path_th)==1)  
    temp1(path_th,:,(tx-1)*Rr+rx)=channeltest1(count((tx-1)*Rr+rx,path_th),temp_noise1,temp_noise2,dot,p,ratio,number);    
    leftnum((tx-1)*Rr+rx,path_th)=length(temp1(path_th,:,(tx-1)*Rr+rx));
    count((tx-1)*Rr+rx,path_th)=count((tx-1)*Rr+rx,path_th)+1;
end

if(leftnum((tx-1)*Rr+rx,path_th)<needleng)    
    temp3=channeltest1(count((tx-1)*Rr+rx,path_th),temp_noise1,temp_noise2,dot,p,ratio,number);
        if ((tx-1)*Rr+rx==1)
            if(path_th==1)
           [M1,M2,M3]=size(temp1);
        loc=find(temp1(path_th,:,(tx-1)*Rr+rx)~=0);
        beginloc=loc(1);
        temp2=temp1(:,beginloc:M2,:);
       temp1=[temp1(:,beginloc:end,:) zeros(path_num,length(temp3),Rr*Tr)];
%clear temp1;
        end  
     end

temp1(path_th,:,(tx-1)*Rr+rx)=[temp2(path_th,:,(tx-1)*Rr+rx) temp3];
    leftnum((tx-1)*Rr+rx,path_th)=leftnum((tx-1)*Rr+rx,path_th)+length(temp3);
      clear temp3;
    count((tx-1)*Rr+rx,path_th)=count((tx-1)*Rr+rx,path_th)+1;
end    
Loc=find(temp1(path_th,:,(tx-1)*Rr+rx)~=0);
Beginloc=Loc(1);
output=temp1(path_th,Beginloc:Beginloc+needleng-1,(tx-1)*Rr+rx);
temp1(path_th,Beginloc:Beginloc+needleng-1,(tx-1)*Rr+rx)=0;
leftnum((tx-1)*Rr+rx,path_th)=leftnum((tx-1)*Rr+rx,path_th)-needleng;











